// pages/signed_detail/signed_detail.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: null,
    markers: [],
    yearMonth: null,  // 当前日历年月 yearMonth
    year: null,
    month: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.userId? options.userId: null
    })
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '打卡记录'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '打卡记录'
    }
  },

  /**
   * 日历组件加载完成, 设置签到记录
   */
  handleCalendarLoad({detail}) {
    // console.log('calendar-load', detail)
    const yearMonth = detail.date.year +'-'+ (detail.date.month>=10? detail.date.month: '0'+ detail.date.month);
    this.setData({
      yearMonth: yearMonth,
      year: detail.date.year,
      month: detail.date.month,
    })
    this.reqHasSignedData();
  },

  /**
   * 日期选择改变
   */
  handleCalendarDateChange({detail}) {
    // console.log(detail)
    let isLoad = false;
    const currDate = detail.date.year +'-'+ (detail.date.month>=10? detail.date.month: '0'+ detail.date.month);
    // 月份不相等, 重新获取数据
    if (this.data.yearMonth && this.data.yearMonth!=currDate) {
      isLoad = true;
    }
    
    this.setData({
      yearMonth: currDate,
      year: detail.date.year,
      month: detail.date.month
    })

    if (isLoad) {
      this.reqHasSignedData();
    }
  },

  /**
   * 请求当前月份已签到的数据
   */
  reqHasSignedData() {
    const param = {
      userId: this.data.userId, year: this.data.year, month: this.data.month
    }
    request.GET('/sign/calDates', param).then(res => {
      if (res.data.resCode==200) {
        this.setData({
          markers: res.data.markersDate
        })
        // 重新设置标记模板
        this.calendar = this.selectComponent('#calendar');
        this.calendar.setMarkers(this.data.markers);
      }
    })
  },
  
})